package sueldos;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Contacto {

	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Contacto cs = new Contacto();
		
		
		//Person p = new Person(1, "pepe", "lavalle 1346");
		//cs.cargaPersona(p);
		//Phone f = new Phone(1,1,"4371-0284");
		//cs.cargaTelefono(f);
		
		
		ResultSet rs = cs.traeContactos();

		
		int j = 9;
		
		try {
			while (rs.next()){
				Person pr = new Person(j, rs.getString("NOMBRE"), rs.getString("direccion"));
				cs.cargaPersona(pr);
				//Phone f = new Phone(j,,rs.getString(3));
				//cs.cargaTelefono(f);
				j++;
			}
			rs.close();
			
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
	}

	
	
	public ResultSet traeContactos(){
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/addisyc","", ""); 
			String sql = "SELECT DISTINCT NOMBRE,CONCAT(DIRECCION,' - ',LOCALIDAD) AS direccion,TELEFONO,codigo FROM proveedo " +
			"WHERE TELEFONO <> ''";

			PreparedStatement ps = conexion.prepareStatement(sql);
			ps.execute();
			return ps.getResultSet();
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	
	
	
	
	
	public void cargaPersona(Person p){
		try {
			String userName = "";
			String password = "";
			String url = "jdbc:odbc:contacto";
			Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
			Connection conn = DriverManager.getConnection (url, userName, password);
			
			String query = "insert into person (Contactnr,Name,Address) values (?,?,?)";
			
			PreparedStatement rs = conn.prepareStatement(query);
			rs.setInt(1, p.getContactnr());
			rs.setString(2, p.getName());
			rs.setString(3, p.getAddress());

			rs.execute();
			rs.close();
			conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	
	public void cargaTelefono(Phone f){
		try {
			String userName = "";
			String password = "";
			String url = "jdbc:odbc:contacto";
			Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
			Connection conn = DriverManager.getConnection (url, userName, password);
			String sql = "insert into telefono(Contactnr,Order,Number) values (?,?,?)";

			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, f.getContactnr());
			ps.setInt(2, 1);
			ps.setString(3, f.getNumber());
			
			ps.execute();
			ps.close();
			conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}


class Person{
	
	int Contactnr;
	String Name;
	String Dateofbirth;
	String Address;
	String Note;
	
	public int getContactnr() {
		return Contactnr;
	}

	public void setContactnr(int contactnr) {
		Contactnr = contactnr;
	}

	public String getName() {
		return Name;
	}

	public void setName(String name) {
		Name = name;
	}

	public String getDateofbirth() {
		return Dateofbirth;
	}

	public void setDateofbirth(String dateofbirth) {
		Dateofbirth = dateofbirth;
	}

	public String getAddress() {
		return Address;
	}

	public void setAddress(String address) {
		Address = address;
	}

	public String getNote() {
		return Note;
	}

	public void setNote(String note) {
		Note = note;
	}

	public Person(){
		
	}
	
	public Person(int contactnr, String name, String address) {
		super();
		Contactnr = contactnr;
		Name = name;
		Address = address;
		
	}
	
	
	
	
}


class Phone{
	
	
	int Contactnr;
	int Order;
	String Number;

	
	public int getContactnr() {
		return Contactnr;
	}
	public void setContactnr(int contactnr) {
		Contactnr = contactnr;
	}
	public int getOrder() {
		return Order;
	}
	public void setOrder(int order) {
		Order = order;
	}
	public String getNumber() {
		return Number;
	}
	public void setNumber(String number) {
		Number = number;
	}
	
	public Phone(){
		
	}
	
	public Phone(int contactnr, int order, String number) {
		super();
		Contactnr = contactnr;
		Order = order;
		Number = number;

	}
	
	
}


/*ResultSet keys = rs.getGeneratedKeys();  

keys.next();  
System.out.println(keys.getInt(1));*/

/*
   public int performKeys(String Query) {  
       PreparedStatement pstmt;  
       int key = 0;  
       try {  
       pstmt = conn.prepareStatement(Query, Statement.RETURN_GENERATED_KEYS);  
          
       pstmt.executeUpdate();  
       ResultSet keys = pstmt.getGeneratedKeys();  
         
       keys.next();  
       key = keys.getInt(1);  
       keys.close();  
       pstmt.close();  
       conn.close();  
       } catch (Exception e) { e.printStackTrace(); }  
       return key;  
       } */
   
   
/*
for (int i = 1; i <= rs.getMetaData().getColumnCount();i++){
	 System.out.println(rs.getMetaData().getColumnLabel(i));
	 
}*/

/*
while (rs.getResultSet().next()){
	 System.out.println(rs.getResultSet().getRow());
	 rm.getColumnName(column);
	 if (rs.getResultSet().getBoolean(3)!=true){
		 TableItem item = new TableItem(table, SWT.NONE);
		 item.setText(0, rs.getResultSet().getString(1));
		 item.setText(1, rs.getResultSet().getString(2));
	 }else{
		 TableItem item = new TableItem(table, SWT.NONE);
		 item.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));

		 item.setText(0, rs.getResultSet().getString(1));
		 item.setText(1, rs.getResultSet().getString(2));
	 }*/
